package qlsl.androiddesign.api.adapter.baseadapter.listviewadapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import qlsl.androiddesign.api.util.commonutil.Log;

/**
 * 类描述：包括ListView和RecyclerView item设置常见属性
 * 需要传入的键：
 * 传入的值类型：
 * 传入的值含义：
 * 是否必传：
 * 作者：郑朝军 on 2017/6/20 10:03
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2017/6/20 10:03
 * 修改备注：
 */
public class ViewHolderHelper
{
    protected final SparseArrayCompat<View> mViews;

    protected View mConvertView;

    protected Context mContext;

    protected int mPosition;

    protected RecyclerViewHolder mRecyclerViewHolder;

    protected RecyclerView mRecyclerView;

    protected AdapterView mAdapterView;

    public ViewHolderHelper(ViewGroup adapterView, View convertView)
    {
        mViews = new SparseArrayCompat<>();
        mAdapterView = (AdapterView) adapterView;
        mConvertView = convertView;
        mContext = convertView.getContext();
    }

    public ViewHolderHelper(RecyclerView recyclerView, RecyclerViewHolder recyclerViewHolder)
    {
        mViews = new SparseArrayCompat<>();
        mRecyclerView = recyclerView;
        mRecyclerViewHolder = recyclerViewHolder;
        mConvertView = mRecyclerViewHolder.itemView;
        mContext = mConvertView.getContext();
    }

    public RecyclerViewHolder getRecyclerViewHolder()
    {
        return mRecyclerViewHolder;
    }

    public void setPosition(int position)
    {
        mPosition = position;
    }

    public int getPosition()
    {
        if (mRecyclerViewHolder != null)
        {
            return mRecyclerViewHolder.getAdapterPositionWrapper();
        }
        return mPosition;
    }

    /**
     * 获取item的根控件
     *
     * @return
     */
    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对应的控件，如果没有则加入mViews，则从item根控件中查找并保存到mViews中
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(@IdRes int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 通过ImageView的Id获取ImageView
     *
     * @param viewId
     * @return
     */
    public ImageView getImageView(@IdRes int viewId)
    {
        return getView(viewId);
    }

    /**
     * 通过TextView的Id获取TextView
     *
     * @param viewId
     * @return
     */
    public TextView getTextView(@IdRes int viewId)
    {
        return getView(viewId);
    }


    /**
     * 设置对应id的控件的文本内容
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolderHelper setText(@IdRes int viewId, CharSequence text)
    {
        if (text == null)
        {
            text = "";
        }
        getTextView(viewId).setText(text);
        return this;
    }

    /**
     * 设置对应id的控件的文本内容
     *
     * @param viewId
     * @param stringResId 字符串资源id
     * @return
     */
    public ViewHolderHelper setText(@IdRes int viewId, @StringRes int stringResId)
    {
        getTextView(viewId).setText(stringResId);
        return this;
    }

    /**
     * 设置对应id的控件的文字大小，单位为 sp
     *
     * @param viewId
     * @param size   文字大小，单位为 sp
     * @return
     */
    public ViewHolderHelper setTextSizeSp(@IdRes int viewId, float size)
    {
        getTextView(viewId).setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        return this;
    }

    /**
     * 设置对应id的控件的文字是否为粗体
     *
     * @param viewId
     * @param isBold 是否为粗体
     * @return
     */
    public ViewHolderHelper setIsBold(@IdRes int viewId, boolean isBold)
    {
        getTextView(viewId).getPaint().setTypeface(isBold ? Typeface.DEFAULT_BOLD : Typeface
                .DEFAULT);
        return this;
    }

    /**
     * 设置对应id的控件的html文本内容
     *
     * @param viewId
     * @param source html文本
     * @return
     */
    public ViewHolderHelper setHtml(@IdRes int viewId, String source)
    {
        if (source == null)
        {
            source = "";
        }
        getTextView(viewId).setText(Html.fromHtml(source));
        return this;
    }

    /**
     * 设置对应id的控件是否选中
     *
     * @param viewId
     * @param checked
     * @return
     */
    public ViewHolderHelper setChecked(@IdRes int viewId, boolean checked)
    {
        Checkable view = getView(viewId);
        view.setChecked(checked);
        return this;
    }

    public ViewHolderHelper setTag(@IdRes int viewId, Object tag)
    {
        View view = getView(viewId);
        view.setTag(tag);
        return this;
    }

    public ViewHolderHelper setTag(@IdRes int viewId, int key, Object tag)
    {
        View view = getView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public ViewHolderHelper setVisibility(@IdRes int viewId, int visibility)
    {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public ViewHolderHelper setImageBitmap(@IdRes int viewId, Bitmap bitmap)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    public ViewHolderHelper setImageDrawable(@IdRes int viewId, Drawable drawable)
    {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    /**
     * @param viewId
     * @param textColorResId 颜色资源id
     * @return
     */
    public ViewHolderHelper setTextColorRes(@IdRes int viewId, @ColorRes int textColorResId)
    {
        getTextView(viewId).setTextColor(mContext.getResources().getColor(textColorResId));
        return this;
    }

    /**
     * @param viewId
     * @param textColor 颜色值
     * @return
     */
    public ViewHolderHelper setTextColor(@IdRes int viewId, int textColor)
    {
        getTextView(viewId).setTextColor(textColor);
        return this;
    }

    /**
     * @param viewId
     * @param backgroundResId 背景资源id
     * @return
     */
    public ViewHolderHelper setBackgroundRes(@IdRes int viewId, int backgroundResId)
    {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundResId);
        return this;
    }

    /**
     * @param viewId
     * @param color  颜色值
     * @return
     */
    public ViewHolderHelper setBackgroundColor(@IdRes int viewId, int color)
    {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    /**
     * @param viewId
     * @param colorResId 颜色值资源id
     * @return
     */
    public ViewHolderHelper setBackgroundColorRes(@IdRes int viewId, @ColorRes int colorResId)
    {
        View view = getView(viewId);
        view.setBackgroundColor(mContext.getResources().getColor(colorResId));
        return this;
    }

    /**
     * @param viewId
     * @param imageResId 图像资源id
     * @return
     */
    public ViewHolderHelper setImageResource(@IdRes int viewId, @DrawableRes int imageResId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }

    /**
     * 设置字体是否为粗体
     *
     * @param viewId
     * @param isBold
     * @return
     */
    public ViewHolderHelper setBold(@IdRes int viewId, boolean isBold)
    {
        getTextView(viewId).getPaint().setTypeface(isBold ? Typeface.DEFAULT_BOLD : Typeface
                .DEFAULT);
        return this;
    }
}
